﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace VersionManage.Common
{
    public class ValidateHelper
    {
        #region 私有变量

        /// <summary>
        /// 正则表达式
        /// </summary>
        private static string regPattern = string.Empty;

        #endregion

        #region 验证邮箱是否正确 ： IsEmail

        /// <summary>
        /// 验证邮箱是否正确
        /// </summary>
        /// <param name="strEmail">要验证的字符串</param>
        /// <returns></returns>
        public static bool IsEmail(string email)
        {
            regPattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            return Regex.IsMatch(email, regPattern);
        }

        #endregion

        #region 验证手机号是否正确,13\15\18 ： IsMobile

        /// <summary>
        /// 验证手机号是否正确,13\15\18
        /// </summary>
        /// <param name="mobile">要验证的字符串</param>
        public static bool IsMobile(string mobile)
        {
            regPattern = @"^1[358]\d{9}$";
            return Regex.IsMatch(mobile, regPattern);
        }

        #endregion

        #region 验证固定电话是否正确,带区号,用"-"分开,第一个字符为"0"开头 ： IsTelephone

        /// <summary>
        /// 验证固定电话是否正确,带区号,用"-"分开,第一个字符为"0"开头
        /// </summary>
        /// <param name="telephone">要验证的字符串</param>
        /// <returns></returns>
        public static bool IsTelephone(string telephone)
        {
            regPattern = @"^\d{3,4}-\d{7,8}$";
            return Regex.IsMatch(telephone, regPattern);
        }

        #endregion

        #region 验证邮政编码是否正确 ： IsPost

        /// <summary>
        /// 验证邮政编码是否正确
        /// </summary>
        /// <param name="post">要验证的字符串</param>
        /// <returns></returns>
        public static bool IsPost(string post)
        {
            regPattern = @"[1-9]\d{5}(?!\d)";
            return Regex.IsMatch(post, regPattern);
        }

        #endregion

        #region 验证身份证是否正确 ： IsIDCard

        /// <summary>
        /// 验证身份证是否正确
        /// </summary>
        /// <param name="iDCard">要验证的字符串</param>
        /// <returns></returns>
        public static bool IsIDCard(string iDCard)
        {
            regPattern = @"[1-9]\d{5}(?!\d)";
            return Regex.IsMatch(iDCard, regPattern);
        }

        #endregion

        #region 验证QQ号码是否正确,从10000开始 ： IsQQ

        /// <summary>
        /// 验证QQ号码是否正确,从10000开始
        /// </summary>
        /// <param name="qq">要验证的字符串</param>
        /// <returns></returns>
        public static bool IsQQ(string qq)
        {
            regPattern = @"[1-9][0-9]{4,}";
            return Regex.IsMatch(qq, regPattern);
        }

        #endregion

        #region 验证IP地址是否正确 ： IsIP

        /// <summary>
        /// 验证IP地址是否正确
        /// </summary>
        /// <param name="Ip">要验证的字符串</param>
        /// <returns></returns>
        public static bool IsIP(string iP)
        {
            regPattern = @"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$";

            bool bl = Regex.IsMatch(iP, regPattern);

            if (bl == true)
            {
                string[] strIpArr = iP.Split('.');

                for (int i = 0; i < strIpArr.Length; i++)
                {
                    if (Convert.ToInt32(strIpArr[i]) > 255)
                    {
                        bl = false;
                        break;
                    }
                }
            }

            return bl;
        }

        #endregion

        #region 验证账号是否合法 ： IsLegalName

        /// <summary>        
        /// 验证帐号是否合法,字母开头，允许3-16字节，允许字母数字下划线
        /// @"^[a-zA-Z][a-zA-Z0-9_]{2,15}$"
        /// </summary>
        /// <param name="name">要验证的字符串</param>
        /// <returns></returns>
        public static bool IsLegalName(string name)
        {
            regPattern = @"^[a-zA-Z][a-zA-Z0-9_]{2,15}$";
            return Regex.IsMatch(name, regPattern);
        }

        #endregion

        #region 验证密码是否合法 ： IsLegalPwd

        /// <summary>
        /// 验证密码是否合法,6-20个字节
        /// </summary>
        /// <param name="pwd">要验证的密码字符串</param>
        /// <returns></returns>
        public static bool IsLegalPwd(string pwd)
        {
            bool bl = false;

            if (pwd.Length >= 6 && pwd.Length <= 20)
            {
                bl = true;
            }
            else
            {
                bl = false;
            }

            return bl;
        }

        #endregion

        #region 验证是否是中文字符 ： IsChinese

        /// <summary>
        /// 验证是否是中文字符
        /// </summary>
        /// <param name="chinese">要验证的字符串</param>
        /// <returns></returns>
        public static bool IsChinese(string chinese)
        {
            regPattern = @"[\u4e00-\u9fa5]";
            return Regex.IsMatch(chinese, regPattern);
        }

        #endregion

        #region 验证是否是空白行 ： IsBlankLine

        /// <summary>
        /// 验证是否是空白行
        ///可以用来删除空白行
        /// </summary>
        /// <param name="blankLine">要验证的字符串</param>
        /// <returns></returns>
        public static bool IsBlankLine(string blankLine)
        {
            regPattern = @"[\n\s*\r]";
            return Regex.IsMatch(blankLine, regPattern);
        }

        #endregion

        #region 验证是否是正确的URL ： IsLegalUrl

        /// <summary>
        /// 验证是否是正确的URL
        /// </summary>
        /// <param name="url">要验证的字符串</param>
        /// <returns></returns>
        public static bool IsLegalUrl(string url)
        {
            regPattern = @"[a-zA-z]+://[^\s]*";
            return Regex.IsMatch(url, regPattern);
        }

        #endregion

        #region 验证数字字符串是否为n位数字 ： IsNumNumber

        /// <summary>
        /// 验证数字字符串是否为n位数字
        /// </summary>
        /// <param name="numString">要验证的数字符串</param>
        /// <param name="num">是多少位</param>
        /// <returns></returns>
        public static bool IsNumNumber(string numString, int num)
        {
            regPattern = @"^[0-9]{" + num.ToString() + "}$";
            return Regex.IsMatch(numString, regPattern);
        }

        #endregion

        #region 验证是否数字 ： IsNumber

        /// <summary>
        /// 验证是否为数字
        /// </summary>
        /// <param name="number">要验证的数字字符串</param>
        /// <returns></returns>
        public static bool IsNumber(string number)
        {
            regPattern = @"^[0-9]\d*$";
            return Regex.IsMatch(number, regPattern);
        }

        #endregion

        #region 验证是否为浮点数(包括整数) ： IsFloat

        /// <summary>
        /// 验证是否为浮点数(包括整数)
        /// </summary>
        /// <param name="strFloat">要验证的数字字符串</param>
        /// <returns></returns>
        public static bool IsFloat(string number)
        {
            regPattern = @"^[0-9]{1,}$|^[0-9]{1,}\.[0-9]{1,}$";
            return Regex.IsMatch(number, regPattern);
        }

        #endregion

        #region 验证是否是数字+",": 
        public static bool IsValidateID(string number)
        {
            regPattern = @"^(\d+,?)+$";
            return Regex.IsMatch(number,regPattern);
        }

        #endregion
    }
}
